//
//  HomeViewController.m
//  SameWorld(merchant)
//
//  Created by 李小朋 on 2017/3/29.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeMenuView.h"
#import "HomeStoreDataView.h"
#import "HomeNewMessageView.h"
#import "HomeNewMessageCell.h"
#import "HomePersonalDetailsView.h"
#import "GoodsManageViewController.h"
#import "EvaluationManageViewController.h"
#import "StoresManageViewController.h"
#import "OrderManageViewController.h"
#import "ScanningViewController.h"
#import "LoseCodeViewController.h"
#import "MineMessageViewController.h"
#import "ReconciliationViewController.h"
#import "ValidationManageViewController.h"
#import "MineViewController.h"
#import "MyPayViewController.h"
#import "RefundViewController.h"
#import "LoginViewController.h"
#import "UserInfo.h"
@interface HomeViewController ()<UITableViewDelegate ,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) HomeMenuView *menuView;
@property (nonatomic ,strong) HomeStoreDataView *storeView;
@property (nonatomic ,strong) HomePersonalDetailsView *personalView;
@property (nonatomic ,strong) UserInfo *userinfo;
@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

    [self setNarBarUI];
    [self createVerificationUI];
    [self createTableViewUI];
    [self createPersonalDetailsUI];
}

- (void)createTableViewUI {

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 74, size_width, size_height - 53 -74 -64) style:(UITableViewStylePlain)];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    
    UIView *headerView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, size_width, 180 + 8 + 115))];
    headerView.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    self.tableView.tableHeaderView = headerView;
    
    NSArray *titleArr = @[@"商品管理",@"评价管理",@"门店管理",@"订单管理",@"验证记录",@"对账查询",@"退款查询",@"更多"];
    NSArray *imageArr = @[@"shangpinguanli",@"pingjiaguanli",@"shangpuguanli",@"dingdanguanli",@"yanzhengjilu",@"duizhangchaxun",@"tuikuanchaxun",@"gengduo"];
    self.menuView = [[HomeMenuView alloc] initWithMenuViewFrame:(CGRectMake(0, 0, size_width, 180)) withTitle:titleArr withImage:imageArr];
    self.menuView.backgroundColor = [UIColor whiteColor];
    [headerView addSubview:self.menuView];
    
    YXWeakSelf
    self.menuView.buttonClicked = ^(NSInteger index) {
        switch (index) {
            case 1000:
            {
                GoodsManageViewController *goodsVC = [[GoodsManageViewController alloc] init];
                [weakSelf.navigationController pushViewController:goodsVC animated:YES];
            }
                break;
            case 1001:
            {
                EvaluationManageViewController *evaluationVC = [[EvaluationManageViewController alloc] init];
                [weakSelf.navigationController pushViewController:evaluationVC animated:YES];
            }
                break;
            case 1002:
            {
                StoresManageViewController *storesVC = [[StoresManageViewController alloc] init];
                [weakSelf.navigationController pushViewController:storesVC animated:YES];
            }
                break;
            case 1003:
            {
                OrderManageViewController *orderVC = [[OrderManageViewController alloc] init];
                [weakSelf.navigationController pushViewController:orderVC animated:YES];
            }
                break;
            case 1004:
            {
                ValidationManageViewController *validation = [[ValidationManageViewController alloc] init];
                [weakSelf.navigationController pushViewController:validation animated:YES];
            }
                break;
            case 1005:
            {
                ReconciliationViewController *reconciliationVC = [[ReconciliationViewController alloc] init];
                [weakSelf.navigationController pushViewController:reconciliationVC animated:YES];
            }
                break;
            case 1006:
            {
                NSLog(@"退款查询");
//                MyPayViewController *payVC = [[MyPayViewController alloc] init];
//                [weakSelf.navigationController pushViewController:payVC animated:YES];
                RefundViewController *refundVC = [[RefundViewController alloc] init];
                [weakSelf.navigationController pushViewController:refundVC animated:YES];
               
            }
                break;
            case 1007:
            {
                NSLog(@"更多");
                [weakSelf showAlertWith:@"该功能尚未开放！"];
            }
                break;
            default:
                break;
        }
        
    };
    
    
    self.storeView = [[HomeStoreDataView alloc] initWithFrame:(CGRectMake(0, self.menuView.frame.size.height + self.menuView.frame.origin.y + 8, size_width, 107))];
    [headerView addSubview:self.storeView];
    

    
    
    // 注册Cell
    [self.tableView registerNib:[UINib nibWithNibName:@"HomeNewMessageCell" bundle:nil] forCellReuseIdentifier:@"HomeNewMessageCell"];
    
}

- (void)createVerificationUI {

    UIView *verificationView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, size_width, 74))];
    verificationView.backgroundColor = [UIColor colorWithHexString:@"#ff9900"];
    [self.view addSubview:verificationView];
    NSArray *titleArr = @[@"扫描验证",@"输码验证"];
    NSArray *imageArr = @[@"saoma",@"shuma"];
    for (int i = 0; i < 2; i ++) {
        UIButton *scanBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        scanBtn.frame = CGRectMake(size_width / 2 * i + size_width/2/2-13, 18, 26, 26);
        [scanBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",imageArr[i]]] forState:(UIControlStateNormal)];
        [scanBtn addTarget:self action:@selector(scanBtnClicked:) forControlEvents:(UIControlEventTouchUpInside)];
        scanBtn.tag = 100 + i;
        [verificationView addSubview:scanBtn];
        
        UILabel *scanLab = [[UILabel alloc] initWithFrame:(CGRectMake(size_width / 2 *i + size_width/2/2 - 25, verificationView.frame.size.height - 28, 50, 20))];
        scanLab.text = titleArr[i];
        scanLab.textColor = [UIColor whiteColor];
        scanLab.textAlignment = NSTextAlignmentCenter;
        scanLab.font = [UIFont systemFontOfSize:12];
        [verificationView addSubview:scanLab];

    }
}


- (void)setNarBarUI {
    
    _userinfo = [UserInfo sharedUserInfo];
    self.title = _userinfo.shopmc;
    
    //1.2.1 在具有导航栏的viewController中设置导航栏背景色
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithHexString:@"#ff9900"]];
    //1.3.1 在具有导航栏的viewController中设置
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    self.navigationItem.rightBarButtonItem =  [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"xinxi"] style:(UIBarButtonItemStyleDone) target:self action:@selector(messageBarItem:)];
    self.navigationItem.hidesBackButton = YES;
    self.navigationItem.leftBarButtonItem = nil;
}


#pragma mark - 消息按钮
- (void)messageBarItem:(UIBarButtonItem *)sender {
    
    NSLog(@"消息");
    MineMessageViewController *messageVC = [[MineMessageViewController alloc] init];
    [self.navigationController pushViewController:messageVC animated:YES];
}
#pragma mark - 扫描按钮
- (void)scanBtnClicked:(UIButton *)sender {
    
    if (sender.tag == 100) {
        ScanningViewController *scanningVC = [[ScanningViewController alloc] init];
        [self.navigationController pushViewController:scanningVC animated:YES];
    }
    
    if (sender.tag == 101) {
        LoseCodeViewController *lostCodeVC = [[LoseCodeViewController alloc] init];
        [self.navigationController pushViewController:lostCodeVC animated:YES];
    }
}

-(void)createPersonalDetailsUI {
    
    self.personalView = [[HomePersonalDetailsView alloc] initWithFrame:(CGRectMake(0,self.tableView.frame.size.height + self.tableView.frame.origin.y, size_width, 53))];
    [self.view addSubview:self.personalView];
    YXWeakSelf
    self.personalView.userCenterBtnClicked = ^{
        // 个人中心
        MineViewController *mineVC = [[MineViewController alloc] init];
        [weakSelf.navigationController pushViewController:mineVC animated:YES];
    };
    
    // 退出按钮
    self.personalView.userExitBtnClicked = ^{
        UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVC];
        [nav.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];   
        keyWindow.rootViewController = nav;
        
    };
}




#pragma mark - UITableView的代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    HomeNewMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeNewMessageCell" forIndexPath:indexPath];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    HomeNewMessageView *newMsgView = [[HomeNewMessageView alloc] initWithFrame:(CGRectMake(0, 0, size_width, 45))];
    
    return newMsgView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 45;
}



@end
